package com.jingan.nursinghome.regulatory;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.dialog.micrometertable.CreateMicrometerTableDailog;
import com.jingan.nursinghome.regulatory.dialog.micrometertable.LoginDialog;
import com.jingan.nursinghome.regulatory.http.Http;
import com.jingan.nursinghome.regulatory.model.Linkup;
import com.jingan.nursinghome.regulatory.view.TabView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;


import java.io.File;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.base.BasePhotoActivity;
import cn.android.support.v7.lib.sin.crown.dialog.PhotoDialog;
import cn.android.support.v7.lib.sin.crown.dialog.ProgressDailog;
import cn.android.support.v7.lib.sin.crown.utils.AppUtils;
import cn.android.support.v7.lib.sin.crown.utils.AssetsUtils;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

public class MainActivity extends BasePhotoActivity {
    public static MainActivity mainActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivity = this;
        BaseApplication.getInstance().setStatusBarDrak(getWindow(), true);
        setContentView(R.layout.activity_main);
    }

    TabView tab1, tab2, tab3;

    @Override
    protected void initUI() {
        tab1 = (TabView) findViewById(R.id.tab1);
        tab1.setCategory(0);
        tab2 = (TabView) findViewById(R.id.tab2);
        tab2.setCategory(1);
        tab3 = (TabView) findViewById(R.id.tab3);
        tab3.setCategory(2);
    }

    @Override
    protected void adapterUI() {
        ProportionUtils.getInstance().adapterOutView(findViewById(R.id.activity_main_constraintLayout), false);
        //ProportionUtils.getInstance().adapterTextView((TextView) findViewById(R.id.txt_title));
        //ProportionUtils.getInstance().adapterTextView((TextView) findViewById(R.id.txt_login_out));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.view_top));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.view_top_line));
    }

    void cacle() {
        //初始化数据
        Http.getInstance().getJly();//获取敬老院数据
        //Http.getInstance().getToplist();//获取题目模块

        //获取评估人员
        Http.getInstance().getLinkUP1();

        CreateMicrometerTableDailog.createMicrometerTableDailog = null;
    }

    @Override
    protected void listener() {
        //登录
        loginOrOut(true);
        //退出登录
        findViewById(R.id.txt_login_out).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginOrOut(false);
            }
        });
        tab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //千分表
                CreateMicrometerTableDailog.getInstance(MainActivity.this);
            }
        });

    }

    //true登录，false退出登录
    private void loginOrOut(boolean login) {
        LoginDialog.getInstance(MainActivity.this, login, new BaseListenner() {
            @Override
            public void onResult(Object result) {
                //MicrometerTable.getInstance().getTopics();//登录成之后，初始化数据
                cacle();
                if (BaseApplication.getInstance().isFirstOpen(MainActivity.this)) {
                    //首次进入程序(加载数据两秒)
                    mainActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressDailog = ProgressDailog.getInstance(mainActivity);
                            tab1.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    progressDailog.dismiss();
                                }
                            }, 2000);
                        }
                    });
                }

            }
        });
    }

    ProgressDailog progressDailog;

    @Override
    protected void onfinish() {
    }

    @Override
    public void onCropResult(File file, Bitmap bitmap) {

    }

    int widht = ProportionUtils.getInstance().adapterInt(110);

    @Override
    public void onSuccess(File file, Bitmap bm) {
        if (PhotoDialog.mBasePhotoListenner != null) {
            int w=bm.getWidth();
            bm.recycle();
            bm = null;
            Bitmap bitmap = AssetsUtils.getInstance().getBitmapFromFile(file.getAbsolutePath(), true, w / widht);//位图
            bitmap = ProportionUtils.getInstance().GeometricCompressionBitmap(bitmap, getResources().getDimension(R.dimen.x100), 1f);
            PhotoDialog.mBasePhotoListenner.onSuccess(file, bitmap);
        }
    }

    @Override
    public void onFailure(String result) {

    }

    @Override
    public void onRequestPermissionsSuccess(int perMissionMode) {
        super.onRequestPermissionsSuccess(perMissionMode);
        if (perMissionMode == perMissionModeCamera) {
            camera();
        } else if (perMissionMode == perMissionModeStorage) {
            if (selectList != null && reslut != null) {
                openPhoto(selectList, reslut);
            }
        }
    }

    @Override
    protected boolean isExit() {
        return true;
    }

    List<LocalMedia> selectList;
    BaseListenner<List<LocalMedia>> reslut;

    //打开本地相册
    public void openPhoto(List<LocalMedia> selectList, BaseListenner<List<LocalMedia>> reslut) {
        this.selectList = selectList;
        this.reslut = reslut;
        if (requestPermissionsStorage(this)) {
            //每行显示4张，最多选择一张图片。
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage()).imageSpanCount(3).maxSelectNum(3).selectionMedia(selectList)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            // 图片选择结果回调
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            // 例如 LocalMedia 里面返回三种 path
            // 1.media.getPath(); 为原图 path
            // 2.media.getCutPath();为裁剪后 path，需判断 media.isCut();是否为 true
            // 3.media.getCompressPath();为压缩后 path，需判断 media.isCompressed();是否为 true
            // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
//            for (int i = 0; i < selectList.size(); i++) {
//                String path = selectList.get(i).getPath();//路径
//                Bitmap bitmap = AssetsUtils.getInstance().getBitmapFromFile(path, true);//位图
//                bitmap = ProportionUtils.getInstance().GeometricCompressionBitmap(bitmap, getResources().getDimension(R.dimen.x100), 1f);
//
//            }
            if (reslut != null) {
                reslut.onResult(selectList);
            }

        }
    }
}
